<?php
	require_once 'bud_mng'.EXT;

	class Bud_revisi extends Bud_mng{
		protected $model_path	= 'bud/m_activity';

		function __key_formatting($number){
			if(strlen($number)==1){
				return '0'.$number;
			}else{
				return $number;
			}
		}

		function index(){
			
//			if($this->role == 'yayasan'){
//				$record= $this->db->query("select a.*, p.name_program, p.key_bud_m_role from bud_m_d_activity a inner join bud_m_program p on a.key_bud_m_program=p.key where key_bud_m_version='$this->versi'"); 
//				
//			}else 
			if($this->role == 'manager' || $this->role=='admin'){

				$content = array ();
				
				$this->db->select_sum('total');
				$this->db->where('LEFT(`key`,1)',$rowkpu->key);
				$this->db->where('SUBSTR(`key`,1,4) ',$rowpu->key);
				$this->db->where('key_bud_m_version',$this->versi);
				$this->db->from('bud_m_d_activity');
				$prog_total = $this->db->get()->row_array();

				
				$this->db->from('bud_m_program');
				$query_prog = $this->db->get();
				
				if ($query_prog->num_rows()>0)
				{
					foreach($query_prog->result() as $rowprog)
					{
						$this->db->select_sum('total');
						$this->db->where('LEFT(`key`,1)',$rowkpu->key);
						$this->db->where('SUBSTR(`key`,1,4) ',$rowpu->key);
						$this->db->where('SUBSTR(`key`,1,7) ',$rowprog->key);
						$this->db->where('key_bud_m_version',$this->versi);
						$this->db->from('bud_m_d_activity');
						$act_total = $this->db->get()->row_array();

						$this->db->where('key_bud_m_program',$rowprog->key);
						$this->db->where('key_bud_m_version',$this->versi);
						$this->db->from('bud_m_d_activity');
						$query_act = $this->db->get();
						if ($query_act->num_rows()>0)
						{
							foreach($query_act->result() as $rowact)
							{
								$content[$rowprog->key.' '.$rowprog->name_program][$rowact->key.' '.$rowact->name_activity]=$rowact;
							}
						}else{
							$content[$rowprog->key.' '.$rowprog->name_program]='';
						}
					}
				}else{
					$content='';
				}
			}

			$this->db->where('key',$this->versi);
			$this->db->from('bud_m_version');
			$qry=$this->db->get()->row_array();
			$row = explode(',',rtrim($qry['desc_version'],','));
			
			foreach($row as $value){
				$change[substr($value,0,strpos($value,':'))]=substr($value,strpos($value,':')+1);
			}

			if($this->role=='admin'){
				$nav = $this->load->view('bud/navigation/administrator/navigation', '', true);
				$view = 'bud/administrator/revision';
			}else if($this->role=='manager'){
				$nav = $this->load->view('bud/navigation/administrator/nav_manager', '', true);
				$view = 'bud/administrator/revision';
			}else if($this->role=='yayasan'){
				$nav = $this->load->view('bud/navigation/administrator/nav_yayasan', '', true);
				$view = 'bud/administrator/revision';
			}else{
				$nav = '';
				$view = 'bud/illegaluser';
			}

			$data = array(
				'application' => $prefix,
				'application_title' => $this->common->get_application_description($prefix),
				'page' => 'Budget_revisi',
				'navigation' => $this->load->view('nav/default', '', true).$nav,
				'content' => $this->load->view($view,array('query'=>$record, 'versi'=>$this->versi, 'change'=> $change,'view_option'=>$this->role,'content'=>$content,'base'=>$this->config->item('base_url').'system/application/modules/'),true),
			);

			$this->load->view('bud/layout/default', $data);

			/*-------------
			if($this->role=='admin'){
				$nav = $this->load->view('bud/navigation/administrator/navigation', '', true);
				$view = 'bud/administrator/revision';
			}else if($this->role=='manager'){
				$nav = $this->load->view('bud/navigation/administrator/nav_manager', '', true);
				$view = 'bud/administrator/revision';
			}else{
				$nav = '';
				$view = 'bud/illegaluser';
			}
			$data = array(
					'application' => $prefix,
					'application_title' => $this->common->get_application_description($prefix),
					'page' => 'Budget_revisi',
					'navigation' => $this->load->view('nav/default', '', true).$this->load->view('bud/navigation/administrator/navigation', '', true),
					'content' => $this->load->view('bud/administrator/revision',array('query'=>$record, 'versi'=>$this->versi, 'change'=> $change,'view_option'=>$this->role,'content'=>$content,'base'=>$this->config->item('base_url').'system/application/modules/'),true),
				);

			$this->load->view('bud/layout/default', $data);*/
		}

		function process_revisi(){
			if($this->uri->segment(4)=='detail'){
				$desc = "";
				foreach($_POST as $key => $val){
					$key = str_replace("_",".",$key);
					
					$newversi = 'v'.$this->__key_formatting(substr($this->versi,1)+1).'/'.substr($this->versi,-4);
					$query = $this->db->query("select * from `bud_m_version` where `key`='$newversi'");

					if (!($query->num_rows() > 0))
					{
					   $execute =  $this->db->query("INSERT INTO `bud_m_version`(`key`) VALUES ('$newversi')");
					} 
					
					$record= $this->db->query("select * from bud_m_d_activity where `key_bud_m_version` ='".$this->versi."' and `key` = '$key'");
					$row = $record->row_array(1);

					$stghthnke2= 0;
					$vals=array();

					if ($row['juli']>0){
						$stghthnke2++;
						$vals[7]=1;
					}
					if ($row['agustus']>0){
						$stghthnke2++;
						$vals[8]=1;
					}
					if ($row['september']>0){
						$stghthnke2++;
						$vals[9]=1;
					}
					if ($row['oktober']>0){
						$stghthnke2++;
						$vals[10]=1;
					}
					if ($row['november']>0){
						$stghthnke2++;
						$vals[11]=1;
					}
					if ($row['desember']>0){
						$stghthnke2++;
						$vals[12]=1;
					}

					$totalstghthn=$row['januari']+$row['februari']+$row['maret']+$row['april']+$row['mei']+$row['juni'];
					$total =  $val['gaji_honor']+$val['barang_tak_habis']+$val['barang_habis']+$val['perjalanan']+$val['jasa_honor_ext'];
					$totalbaru =  $val['gaji_honor']+$val['barang_tak_habis']+$val['barang_habis']+$val['perjalanan']+$val['jasa_honor_ext']-$totalstghthn;
					
					$perbulan = $stghthnke2!=0?$totalbaru/$stghthnke2:0;
					
					$temp_bulan = array();
					$temp_bulan['januari']=$row['januari'];
					$temp_bulan['februari']=$row['februari'];
					$temp_bulan['maret']=$row['maret'];
					$temp_bulan['april']=$row['april'];
					$temp_bulan['mei']=$row['mei'];
					$temp_bulan['juni']=$row['juni'];
					//meta data for month
					for($i=7;$i<=12;$i++){
						$temp_bulan[$this->__int_to_month($i)]= $vals[$i]!=false?$perbulan:0;
					}

					$newversi = 'v'.$this->__key_formatting(substr($this->versi,1)+1).'/'.substr($this->versi,-	4);
					
					$record= $this->db->query("select * from bud_m_d_activity where `key_bud_m_version` ='".$this->versi."' and `key` = '$key'");
					$row = $record->row_array(1);

					if($total != $row['total']){
						$insertdata = array(
								'key'=> $row['key'],
								'name_activity'=>$row['name_activity'],
								'desc_activity'=>$row['desc_activity'],
								'total'=>$total,
								'gaji_honor'=>$val['gaji_honor'],
								'barang_tak_habis'=>$val['barang_tak_habis'],
								'barang_habis'=>$val['barang_habis'],
								'perjalanan'=>$val['perjalanan'],
								'jasa_honor_ext'=>$val['jasa_honor_ext'],
								'operational'=>$row['operational'],
								'key_bud_m_yayasan_activity'=>$row['key_bud_m_yayasan_activity'],
								'key_bud_r_sumber'=>$row['operational'],
								'key_bud_r_unit'=>$row['key_bud_r_unit'],
								'key_bud_m_version'=>$newversi,
								'key_bud_m_program'=>$row['key_bud_m_program'],
						);

						$insertdata = array_merge($insertdata,$temp_bulan);
						$desc .= $row['key'].":".$row['total'].",";
						$model = $this->load->model($this->model_path);
						$this->common->who_am_i($insertdata, true, 'crea');
						$model->add($insertdata);
					}else{
						$insertdata = array(
								'key'=> $row['key'],
								'name_activity'=>$row['name_activity'],
								'desc_activity'=>$row['desc_activity'],
								'total'=>$row['total'],
								'gaji_honor'=>$val['gaji_honor'],
								'barang_tak_habis'=>$val['barang_tak_habis'],
								'barang_habis'=>$val['barang_habis'],
								'perjalanan'=>$val['perjalanan'],
								'jasa_honor_ext'=>$val['jasa_honor_ext'],
								'operational'=>$row['operational'],
								'key_bud_m_yayasan_activity'=>$row['key_bud_m_yayasan_activity'],
								'key_bud_r_sumber'=>$row['operational'],
								'key_bud_r_unit'=>$row['key_bud_r_unit'],
								'key_bud_m_version'=>$newversi,
								'key_bud_m_program'=>$row['key_bud_m_program'],
						);

						$insertdata = array_merge($insertdata,$temp_bulan);
						$model = $this->load->model($this->model_path);
						$this->common->who_am_i($insertdata, true, 'crea');
						$model->add($insertdata);
					}
				}
				$this->db->query("UPDATE `bud_m_version` SET `desc_version`='$desc' WHERE `key`='$newversi'");
				redirect('bud/bud_revisi');
			}

		}
	}
?>